class Solution {
public:
    vector<string> removeSubfolders(vector<string>& folder) {
        sort(folder.begin(), folder.end());

        for (auto& f: folder) f += '/';

        vector<string> res;
        for (auto& f: folder) {
            if (res.empty() || res.back().size() > f.size() || f.substr(0, res.back().size()) != res.back())
                res.push_back(f);
        }

        for (auto& f: res) f.pop_back();
        return res;
    }
};